package com.gwt.tirso.gymkhana.client;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.jdt.internal.compiler.ast.ArrayAllocationExpression;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class ContestStep3 implements EntryPoint {

	private Label questNumber = new Label();
	private Label questLb = new Label();
	private Label answerLb = new Label();
	private Label question = new Label();
	private TextBox answerBox = new TextBox();
	private Button nextBt = new Button("Aceptar");
	private VerticalPanel mainPanel = new VerticalPanel();
	private Label errorLabel = new Label("");

	private String answer = new String("");

	private String code;
	private int questN;
	private Gymkhana g;
	private List<Question> lq;
	private List<String> lqR = new ArrayList<String>();

	private IContestServiceAsync contestCon;

	@Override
	public void onModuleLoad() {

		loadContestStep3();

	}
	

	private void loadContestStep3() {

		contestCon = ConnectionRpc.initRpcContest();
		if(lq == null){
			//lqR = g.getListQReaded();
			contestCon.extractQuestionList(g, new AsyncCallback<List<Question>>() {

				@Override
				public void onSuccess(List<Question> result) {
					if(result != null){
						lq = result;
						loadContest();
					}else{
						errorLabel.setText("Fallo en la conexión con la lista de preguntas");
					}
				
				}

				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub

				}
			});

		}else{
			loadContest();
		}
		
	}

	private void loadContest() {
		
		lqR = g.getListQReaded();
		// We are in the initial question
		if (questN == 0) {
			// We need to search it
			for (int i = 0; i < lq.size(); i++) {
				if (lq.get(i).getCode().equals("0000")) {

					question.setText(lq.get(i).getQuestionText());
					answer = lq.get(i).getAnswerText();

					lqR.add(lq.get(i).getCode());
					lq.remove(i);

					questN++;
				}
			}
		} else {
			// in this way, we bring always the questions in the same order. We
			// need
			// a random value for make the game more interesting
			for (int i = 0; i < lq.size(); i++) {
				if (lq.get(i).getCode().equals(code)) {
					question.setText(lq.get(i).getQuestionText());
					answer = lq.get(i).getAnswerText();

					lqR.add(lq.get(i).getCode());
					lq.remove(i);
					questN++;
				}
			}
		}

		g.setListQReaded(lqR);

		questNumber.setText("Pregunta numero: " + String.valueOf(questN));
		mainPanel.add(questNumber);

		mainPanel.add(questLb);
		mainPanel.add(question);

		mainPanel.add(answerLb);
		mainPanel.add(answerBox);

		mainPanel.add(nextBt);

		nextBt.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				nextStep();

			}
		});

		mainPanel.add(errorLabel);

		mainPanel.addStyleName("contestNext");
		RootPanel.get("contest").add(mainPanel);
	}

	protected void nextStep() {
		if (answerBox.getText().equals(answer)) {
			if (lq.size() > 0) {
				ContestStep4 c4 = new ContestStep4(g, questN, lq);
				c4.onModuleLoad();
				RootPanel.get("contest").remove(mainPanel);
			} else {
				ContestStep5 c5 = new ContestStep5();
				c5.onModuleLoad();
				RootPanel.get("contest").remove(mainPanel);
			}
		} else {
			errorLabel
					.setText("No es correcto, intentalo de nuevo. Asegurate de que este bien escrito con tildes y todo.");
			answerBox.setText("");
			answerBox.setFocus(true);
		}

	}

	public void setGymkhana(Gymkhana result) {
		g = result;
	}

	public ContestStep3(Gymkhana gymk, List<Question> lq, int nQuestion,
			String nextCode) {
		questN = nQuestion;
		g = gymk;
		code = nextCode;
		this.lq = lq;
	}

}
